﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Runtime.Caching;

using CooWork.Models;
using Coowork.Data.Mapping;

namespace CooWork.Services
{
    public class TaskAuth:ITaskAuth
    {
        private CooWorkEntities context = new CooWorkEntities();

        public TaskEditableRole UserRoleForSpecTask(int taskId,int cid)
        {
            MembershipUser mu = Membership.GetUser();
            string uid = mu.ProviderUserKey.ToString();
            string taskCacheStr = string.Format("Task_{0}_{1}_{2}", taskId, cid, mu.ProviderUserKey.ToString());

            var obj = MemoryCache.Default.Get(taskCacheStr);

            if (obj != null)
            {
                return (TaskEditableRole)obj;
            }

            CacheItemPolicy policy = new CacheItemPolicy();
            policy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(10.0);

            if (Roles.IsUserInRole("Admin"))
            {          
                MemoryCache.Default.Add(new CacheItem(taskCacheStr, TaskEditableRole.AdminGroup), policy);
                return TaskEditableRole.AdminGroup;
            }

            //var task = from c in context.CooTasks
            //           where ("," + c.EditableList + ",").Contains("," + mu.ProviderUserKey.ToString() + ",") ||
            //           ("," + c.ObserverList + ",").Contains("," + mu.ProviderUserKey.ToString() + ",")
            //           select c;


            MemoryCache.Default.Add(new CacheItem(taskCacheStr, TaskEditableRole.Editor), policy);
            return TaskEditableRole.Creator;

            //待更新

            //var task = context.CooTasks.Where(x => x.cid == cid).Where(x => ("," + x.EditableList + ",").Contains("," + uid + ",") || ("," + x.ObserverList + ",").Contains("," + uid + ",") || x.Creator.Value == (Guid)mu.ProviderUserKey || x.Owner.Value == (Guid)mu.ProviderUserKey)
            //    .Select(x => new { eflag = ("," + x.EditableList + ",").Contains("," + uid + ","), wflag = ("," + x.ObserverList + ",").Contains("," + uid + ","), cflag = x.Creator.Value == (Guid)mu.ProviderUserKey, oflag = x.Owner.Value == (Guid)mu.ProviderUserKey }).ToList();


            //if (task.Count > 0)
            //{
            //    if (task[0].eflag)
            //    {
            //        MemoryCache.Default.Add(new CacheItem(taskCacheStr, TaskEditableRole.Editor), policy);
            //        return TaskEditableRole.Editor;
            //    }
            //    if (task[0].cflag)
            //    {
            //        MemoryCache.Default.Add(new CacheItem(taskCacheStr, TaskEditableRole.Creator), policy);
            //        return TaskEditableRole.Creator;
            //    }
            //    if (task[0].oflag)
            //    {
            //        MemoryCache.Default.Add(new CacheItem(taskCacheStr, TaskEditableRole.Owner), policy);
            //        return TaskEditableRole.Owner;
            //    }
            //    if (task[0].wflag)
            //    {
            //        MemoryCache.Default.Add(new CacheItem(taskCacheStr, TaskEditableRole.Observer), policy);
            //        return TaskEditableRole.Observer;
            //    }


          //  }

        


          //  MemoryCache.Default.Add(new CacheItem(taskCacheStr, TaskEditableRole.Denyed), policy);
         //   return TaskEditableRole.Denyed;
        }

       




    }
}